package com.itheima.controller;

import com.github.pagehelper.PageInfo;
import com.itheima.entity.Result;
import com.itheima.pojo.SysDictType;
import com.itheima.service.SysDictTypeService;
import com.itheima.vo.SysDictTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典类型接口
 */
@Controller
@RequestMapping("/sysDictType")
public class SysDicTypeController {

    @Autowired
    private SysDictTypeService sysDictTypeService;

    /**
     * 添加字典类型
     * @param sysDictTypeVo
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody SysDictTypeVo sysDictTypeVo){
        try {
            sysDictTypeService.add(sysDictTypeVo);
            return new Result("success","添加成功");
        } catch (Exception e) {
            return new Result("fail","添加失败");
        }
    }

    /**
     * 批量删除字典类型
     * @param sysDictTypeVoList
     * @return
     */
    @PostMapping("/deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestBody List<SysDictTypeVo> sysDictTypeVoList){
        try {
            sysDictTypeService.deleteBatch(sysDictTypeVoList);
            return new Result("success","批量删除成功");
        } catch (Exception e) {
            return new Result("fail","批量删除失败");
        }
    }

    /**
     * 条件查询获取字典类型列表
     * @param sysDictTypeVo
     * @return
     */
    @PostMapping("/getList")
    @ResponseBody
    public Result getList(@RequestBody(required = false) SysDictTypeVo sysDictTypeVo){
      PageInfo pageInfo= sysDictTypeService.list(sysDictTypeVo);
      return new Result("success",pageInfo);
    }

    @PostMapping("/edit")
    @ResponseBody
    public Result edit(@RequestBody SysDictTypeVo sysDictTypeVo){
        try {
            sysDictTypeService.edit(sysDictTypeVo);
            return new Result("success","修改成功");
        } catch (Exception e) {
            return new Result("fail","修改失败");
        }
    }
}
